/**
 * Description: socket 客户端-使用write发送信息
 * File: client.c |Time: 2021-04-09 19:26
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[])
{
    if (argc < 2) {
        fprintf(stderr, "Usage %s <msg>", argv[0]);
        return -1;
    }

    // 使用socket函数获取socket文件描述符
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    // connect连接服务器
    struct sockaddr_in sockaddr;
    struct in_addr server_ip;
    if (inet_aton("192.168.1.145", &server_ip) == 0) {
        printf("invalid address");
        return -1;
    }

    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(1122);
    sockaddr.sin_addr = server_ip;

    connect(sockfd, (struct sockaddr *)&sockaddr, sizeof(struct sockaddr));

    // 发送信息
    write(sockfd, argv[1], strlen(argv[1]));

    return 0;
}


